package org.aeon.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author: MissingAeon
 * @date: 2024/1/25 18:29
 * @description:
 */
@Slf4j
public class MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;

    static {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

    public static SqlSession getSqlSession(boolean auto) {
        return sqlSessionFactory.openSession(auto);
    }

    public static SqlSession getSqlSession(ExecutorType type, boolean auto) {
        return sqlSessionFactory.openSession(type, auto);
    }
}
